package com.heima.job.task;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DemoTask {

    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");
        System.out.println("invoked......");
    }


    @XxlJob("sharding")
    public void shardingJobHandler() throws Exception {

        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex(); // 获取当前任务需要处理的分片
        int shardTotal = XxlJobHelper.getShardTotal(); // 获取总的分片数量

        List<Integer> list = getList();

        //分片其中的5千个任务给当前的task去执行
        for (Integer num : list) {
            if(num % shardTotal == shardIndex){
                System.out.println(num);
            }
        }

    }

    public List<Integer> getList(){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }
}
